﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class onlineMessageHandler : IHttpHandler, IRequiresSessionState
    {

        readonly BasePage thePage = new BasePage();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

      
        tbOnlineMessage oOnlineMessage = new tbOnlineMessage();

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            try
            {
                if (string.Compare(context.Request.Form["code"], context.Session["omCheckCode"].ToString(), true) != 0)
                {
                    context.Response.Write("0");
                }
                else
                {
                    if (context.Session["user"] != null)
                    {
                        this.oOnlineMessage.messageUser = (context.Session["user"] as tbUser).userName;
                    }
                    else
                    {
                        this.oOnlineMessage.messageUser = "游客";
                    }
                    this.oOnlineMessage.title = context.Request.Form["title"];
                    this.oOnlineMessage.content = context.Request.Form["content"];
                    this.oOnlineMessage.contact = context.Request.Form["contact"];
                    this.oOnlineMessage.messageIP = new ClinetMAC().GetIPAddress();
                    if (BaseFactory.theOnlineMessage.Add(this.oOnlineMessage) > 0)
                    {
                        context.Response.Write("1");
                    }
                    else
                    {
                        context.Response.Write("2");
                    }
                }
            }
            catch
            {
                context.Response.Write("2");
            }
        }

        #endregion
    }
}
